package com.gift.common;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

public class TimeHandler {

    public static Map<String,Object> findParmHandler(Map<String,String> map){
        Map<String,Object> mapO = new HashMap<>();
        LocalDate toDay = LocalDate.now();
        int nowShift =TimeHandler.getShift();


        DateTimeFormatter sf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime toDayBegin = LocalDateTime.parse(toDay.toString()+" 00:00:00", sf);

        LocalDateTime workTimeBegin = toDayBegin.plusHours(8);
        LocalDateTime workTimeEnd = workTimeBegin.plusHours(24);

        if(nowShift == 2){
            workTimeBegin = toDayBegin;
            workTimeEnd = toDayBegin.plusHours(8);
        }
        if(map.get("ssbegin") == null || map.get("ssbegin") == ""){
            mapO.put("ssbegin",workTimeBegin);

        }else{
            String ssbegin = map.get("ssbegin");
            ssbegin += " 00:00:00";
            LocalDateTime workTimeBeginDay = LocalDateTime.parse(ssbegin, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            mapO.put("ssbegin",workTimeBeginDay.plusHours(8));
        }
        if(map.get("ssend") == null || map.get("ssend") == ""){
            mapO.put("ssend",workTimeEnd);
        }else{
            String ssend = map.get("ssend");
            ssend += " 00:00:00";
            LocalDateTime workTimeBeginDay = LocalDateTime.parse(ssend, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            mapO.put("ssend",workTimeBeginDay.plusHours(32));
        }

        if(map.get("handlerid")==null || map.get("handlerid").equals("")){
            mapO.put("handlerid",null);
            return mapO;
        }
        int hid = Integer.valueOf(map.get("handlerid"));
        mapO.put("handlerid",hid);
        return mapO;
    }


    public static int getShift(){
        //八点到下午4点是早班，16点到零点是中班。零点到第二天8点是晚班
        LocalTime workTimeBegin = LocalTime.MIN.plusHours(8);
        //早班开始时间,16:00
        LocalTime zaoban = workTimeBegin.plusHours(8);
        //早班结束时间，中班开始时间,00:00
        LocalTime zhongban = zaoban.plusHours(8).minusSeconds(5);
        //中班结束时间，晚班开始时间,08:00
        LocalTime wanban = zhongban.plusHours(8).plusSeconds(5);
        //现在时间
        LocalTime nowTime = LocalTime.now();
        if(nowTime.isAfter(workTimeBegin) && nowTime.isBefore(zaoban)){
            return 0;
        }
        if(nowTime.isAfter(zaoban) && nowTime.isBefore(zhongban)){
            return 1;
        }
        return 2;
    }





}
